/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.schedul.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.schedul.entity.SchedulingInformation;
import com.thinkgem.jeesite.modules.schedul.service.SchedulingInformationService;
import com.thinkgem.jeesite.modules.shift.entity.Shift;
import com.thinkgem.jeesite.modules.shift.service.ShiftService;

/**
 * 员工排班表Controller
 * 
 * @author pan.zhou
 * @version 2017-07-15
 */
@Controller
@RequestMapping(value = "${adminPath}/schedul/schedulingInformation")
public class SchedulingInformationController extends BaseController {

	@Autowired
	private SchedulingInformationService schedulingInformationService;

	@Autowired
	private ShiftService shiftService;

	@ModelAttribute
	public SchedulingInformation get(@RequestParam(required = false) String id) {
		SchedulingInformation entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = schedulingInformationService.get(id);
		}
		if (entity == null) {
			entity = new SchedulingInformation();
		}
		return entity;
	}

	@RequiresPermissions("schedul:schedulingInformation:view")
	@RequestMapping(value = { "list", "" })
	public String list(SchedulingInformation schedulingInformation,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		Page<SchedulingInformation> page = schedulingInformationService
				.findPage(new Page<SchedulingInformation>(request, response),
						schedulingInformation);
		model.addAttribute("page", page);
		return "modules/schedul/schedulingInformationList";
	}

	@RequiresPermissions("schedul:schedulingInformation:view")
	@RequestMapping(value = "form")
	public String form(SchedulingInformation schedulingInformation, Model model) {
		model.addAttribute("schedulingInformation", schedulingInformation);
		return "modules/schedul/schedulingInformationAddForm";
	}

	/**
	 * 数据修改时调用
	 * 
	 * @param schedulingInformation
	 * @param model
	 * @return
	 */
	@RequiresPermissions("schedul:schedulingInformation:view")
	@RequestMapping(value = "updateForm")
	public String updateForm(SchedulingInformation schedulingInformation,
			Model model) {
		model.addAttribute("schedulingInformation", schedulingInformation);
		return "modules/schedul/schedulingInformationForm";
	}

	/**
	 * 新增
	 * 
	 * @param schedulingInformation
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("schedul:schedulingInformation:edit")
	@RequestMapping(value = "save")
	public String save(SchedulingInformation schedulingInformation,
			Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, schedulingInformation)) {
			return form(schedulingInformation, model);
		}
		schedulingInformationService.save(schedulingInformation);
		addMessage(redirectAttributes, "保存员工排班成功");
		return "redirect:" + Global.getAdminPath()
				+ "/schedul/schedulingInformation/?repage";
	}

	/**
	 * 新增及修改
	 * 
	 * @param schedulingInformation
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("schedul:schedulingInformation:edit")
	@RequestMapping(value = "update")
	public String update(SchedulingInformation schedulingInformation,
			Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, schedulingInformation)) {
			return form(schedulingInformation, model);
		}
		String oldShiftId = schedulingInformation.getOldShiftId();
		String shiftId = schedulingInformation.getShift().getId();
		
		// 判断是否选择同一个班次
		if (shiftId.equals(oldShiftId)) {
			addMessage(redirectAttributes, "请不要选择同一班次");
			return "redirect:" + Global.getAdminPath()
					+ "/schedul/schedulingInformation/?repage";
		}
		
		// 判断日期
		if (schedulingInformation.getStarttime().getTime() > schedulingInformation
				.getEndtime().getTime()) {
			addMessage(redirectAttributes, "开始日期不能大于结束日期");
			return "redirect:" + Global.getAdminPath()
					+ "/schedul/schedulingInformation/?repage";
		}

		// 根据ID查询班次信息
		Shift shift = shiftService.get(oldShiftId);
		// 班次日期不能大于选择班次结束日期
		if (schedulingInformation.getEndtime().getTime() > shift.getEnddate()
				.getTime()) {
			addMessage(redirectAttributes, "班次日期不能大于选择班次结束日期");
			return "redirect:" + Global.getAdminPath()
					+ "/schedul/schedulingInformation/?repage";
		}

		// 执行业务方法
		schedulingInformationService.update(schedulingInformation);
		addMessage(redirectAttributes, "保存员工排班成功");
		return "redirect:" + Global.getAdminPath()
				+ "/schedul/schedulingInformation/?repage";
	}

	@RequiresPermissions("schedul:schedulingInformation:edit")
	@RequestMapping(value = "delete")
	public String delete(SchedulingInformation schedulingInformation,
			RedirectAttributes redirectAttributes) {
		schedulingInformationService.delete(schedulingInformation);
		addMessage(redirectAttributes, "删除员工排班成功");
		return "redirect:" + Global.getAdminPath()
				+ "/schedul/schedulingInformation/?repage";
	}

	/**
	 * 异步调用,根据考勤时段进行排版
	 * 
	 * @param schclass
	 *            考勤时段ID
	 * @param employeeList
	 *            员工ID，例如：123,456
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = { "saveData" })
	public Map<String, Object> saveData(String schclassId,
			String employeeNumber, HttpServletRequest request,
			HttpServletResponse response) {
		response.setContentType("application/json; charset=UTF-8");
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			schedulingInformationService.saveData(schclassId, employeeNumber);
			resultMap.put("success", true);
		} catch (Exception e) {
			resultMap.put("success", false);
			e.printStackTrace();
		}
		return resultMap;
	}

}